<template>
  <view v-if="memberId === 0" class="container auth">
    <view class="issale" @click="onToProfile">成为销售</view>
  </view>
 <view v-else class="container">
    <u-button size="default" type="success" style="margin-bottom: 40rpx" @click="onScanCode" >扫码</u-button>
    <view class="records">
      <view class="records-item">
        <view class="time">日期</view>
        <view class="score">积分</view>
        <view class="score t-right">商品编码</view>
      </view>
      <view v-for="(item, i) in records" :key="i" class="records-item">
        <view class="time">{{ item.createTime }}</view>
        <view class="score">{{ item.integral }}</view>
        <view class="score">{{ item.baijiuCode }}</view>
      </view>
    </view>
  </view>
</template>

<script>
  import {
    mapGetters
  } from 'vuex'
  export default {
    data() {
      return {
        records: [],
        isEnd: false,
        page: 1,
      }
    },
    computed: {
      ...mapGetters(['memberId'])
    },
    onLoad() {
      this.getRecord()
    },
    onReachBottom() {
      this.onNextPage()
    },
    methods: {
      onScanCode() { // 扫码核销
        wx.scanCode({
          success: res => {
            console.log(res)
            const { errMsg, result } = res
            if (errMsg === "scanCode:ok" && result) {
              this.onWriteOff(result.replace(/"/g, ''))
            }
          }
        })
      },
      onWriteOff(code) {
        this.$http({
          url: '/member/qrCodeIntegral',
          data: { code }
        }).then(res => {
          this.$u.toast('积分成功')
          this.initRecord()
        }).catch(err => {
          this.$u.toast(err.msg || '积分失败')
        })
      },
      initRecord() {
        this.page = 1
        this.records = []
        this.getRecord()
      },
      onNextPage() {
        if (this.isEnd != 1) {
          this.page ++
          this.getRecord()
        }
      },
      getRecord() {
        this.$http({
          url: '/member/getMyGoodsCodeList',
          data: {
            page: this.page,
          }
        }).then(res => {
          this.records = this.records.concat(res.data.integralLogList)
          this.isEnd = res.data.isEnd
        })
      },
     
      onToProfile() {
        wx.switchTab({
          url: '../my/index'
        })
      },
      
    },
  }
</script>

<style lang="less">
  .container {
    padding: 20rpx;
    background: #fafafa url(https://qncdn2.dianhetech.com/wenzunbg.jpg) no-repeat;
    background-size: 100% 100%;
    min-height: 100vh;
    &.auth{
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .records{
      padding: 20rpx 0;
    }
    .records-item{
      margin-bottom: 20rpx;
      background: #fff;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 30rpx;
      line-height: 1;
      text-align: center;
    }
    .time{
      font-size: 28rpx;
      color: #666;
      flex: 1 0;
    }
    .score{
      font-size: 30rpx;
      color: #111;
      flex: 1 0;
    }
  }
  .issale{
    color: #ffffff;
    border-color: #19be6b;
    background-color: #19be6b;
    font-size: 40rpx;
    height: 180rpx;
    line-height: 180rpx;
    border-radius: 10rpx;
    width: 600rpx;
    text-align: center;
  }
</style>
